﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Text;
using System.Net;
using System.Xml;

namespace Dropthings.Widget.Widgets.ComWeather
{
    /// <summary>天气</summary>
    public enum WeatherType
    {
        晴 = 0,
        多云 = 1,
        阴 = 2,
        阵雨 = 3,
        雷阵雨 = 4,
        雷阵雨伴有冰雹 = 5,
        雨夹雪 = 6,
        小雨 = 7,
        中雨 = 8,
        大雨 = 9,
        暴雨 = 10,
        大暴雨 = 11,
        特大暴雨 = 12,
        阵雪 = 13,
        小雪 = 14,
        中雪 = 15,
        大雪 = 16,
        暴雪 = 17,
        雾 = 18,
        冻雨 = 19,
        沙尘暴 = 20,
        小雨_中雨 = 21,
        中雨_大雨 = 22,
        大雨_暴雨 = 23,
        暴雨_大暴雨 = 24,
        大暴雨_特大暴雨 = 25,
        小雪_中雪 = 26,
        中雪_大雪 = 27,
        大雪_暴雪 = 28,
        浮尘 = 29,
        扬沙 = 30,
        强沙尘暴 = 31
    }



    /// <returns>天气预报类</returns>
    public class Weather
    {
        private const string __Url = "http://www.weather.com.cn/html/weather/{0}.shtml";
        /// <summary>日期</summary>
        public DateTime Date { get; set; }
        /// <summary>城市</summary>
        public string City { get; set; }
        /// <summary>天气</summary>
        public WeatherType WeatherType1 { get; set; }
        /// <summary>天气</summary>
        public WeatherType WeatherType2 { get; set; }
        /// <summary>温度</summary>
        public string Temperature { get; set; }
        /// <summary>最高温度</summary>
        public int TemperatureMax { get; set; }
        /// <summary>最低温度</summary>
        public int TemperatureMin { get; set; }
        /// <summary>风</summary>
        public string Wind { get; set; }

        /// <summary>穿衣指数</summary>
        public Exponential Costume { get; set; }
        /// <summary>晨练指数</summary>
        public Exponential MorningExercise { get; set; }
        /// <summary>舒适度指数</summary>
        public Exponential Comfort { get; set; }
        /// <summary>感冒指数</summary>
        public Exponential Cold { get; set; }
        /// <summary>洗车指数</summary>
        public Exponential CarWash { get; set; }
        /// <summary>交通指数</summary>
        public Exponential Traffic { get; set; }
        /// <summary>紫外线强度指数</summary>
        public Exponential Ultraviolet { get; set; }
        /// <summary>空气污染扩散条件指数</summary>
        public Exponential AirPollution { get; set; }

        /// <summary>获取天气预报</summary>
        /// <param name="doc">记录天气预报的Xml文档</param>
        /// <returns>天气预报</returns>
        public static Weather Get(XmlDocument doc)
        {
            var weather = new Weather();
            var box_contenttodayinwea = doc.DocumentElement.SelectNodes("//div[@class='box_contenttodayinwea']");
            weather.City = doc.DocumentElement.SelectSingleNode("//div[@class='left_contenttopl']/h3/strong").InnerText;
            var tempDate = doc.DocumentElement.SelectSingleNode("//dt[@class='date']").InnerText;
            weather.Date = DateTime.Parse(tempDate.Split(new char[] { ' ' })[0].Replace("今天是", "").Trim());

            var content1 = box_contenttodayinwea[0];
            var content2 = box_contenttodayinwea[1];
            var content3 = box_contenttodayinwea[2];
            var content4 = box_contenttodayinwea[3];
            var content5 = box_contenttodayinwea[4];
            var weatherTypes = content1.SelectSingleNode("p/em[1]").InnerText.Trim().Replace("-", "_").Split(new string[] { "转" }, StringSplitOptions.RemoveEmptyEntries);
            weather.WeatherType2 = weather.WeatherType1 = (WeatherType)Enum.Parse(typeof(WeatherType), weatherTypes[0]);
            if (weatherTypes.Length > 1)
            {
                weather.WeatherType2 = (WeatherType)Enum.Parse(typeof(WeatherType), weatherTypes[1]);
            }
            weather.Temperature = content1.SelectSingleNode("p/em[2]").InnerText.Trim();
            var temperatureTemp = weather.Temperature.Replace("℃", "").Split('/');
            weather.TemperatureMax = int.Parse(temperatureTemp[0].Trim());
            weather.TemperatureMin = int.Parse(temperatureTemp[1].Trim());
            weather.Wind = content1.SelectSingleNode("p/em[3]").InnerText;
            weather.Costume = Exponential.Get(content2.SelectSingleNode("dl[1]"));
            weather.MorningExercise = Exponential.Get(content2.SelectSingleNode("dl[2]"));
            weather.Comfort = Exponential.Get(content3.SelectSingleNode("dl[1]"));
            weather.Cold = Exponential.Get(content3.SelectSingleNode("dl[2]"));
            weather.CarWash = Exponential.Get(content4.SelectSingleNode("dl[1]"));
            weather.Traffic = Exponential.Get(content4.SelectSingleNode("dl[2]"));
            weather.Ultraviolet = Exponential.Get(content5.SelectSingleNode("dl[1]"));
            weather.AirPollution = Exponential.Get(content5.SelectSingleNode("dl[2]"));
            return weather;
        }
        /// <summary>获取天气预报</summary>
        /// <param name="url">天气预报地址</param>
        /// <returns>天气预报</returns>
        public static Weather Get(string url)
        {
            using (WebClient wc = new WebClient())
            {
                wc.Encoding = Encoding.UTF8;
                Sgml.SgmlReader reader = new Sgml.SgmlReader();
                XmlDocument doc = new XmlDocument();
                string str = wc.DownloadString(url);
                reader.InputStream = new StreamReader(new MemoryStream(Encoding.UTF8.GetBytes(str)));
                reader.DocType = "HTML";
                doc.LoadXml(reader.ReadOuterXml().Replace("xmlns=\"http://www.w3.org/1999/xhtml\"", ""));
                return Get(doc);
            }
        }

        /// <summary>获取天气预报</summary>
        /// <param name="name">城市代码</param>
        /// <returns>天气预报</returns>
        public static Weather GetById(string id)
        {
            return Get(string.Format(__Url, id));
        }
        public override string ToString()
        {
            var sb = new StringBuilder();
            sb.AppendFormat("日期:{0}\r\n", this.Date);
            sb.AppendFormat("城市:{0}\r\n", this.City);
            if (this.WeatherType1 == this.WeatherType2)
            {
                sb.AppendFormat("天气:{0}\r\n", this.WeatherType1);
            }
            else {
                sb.AppendFormat("天气:{0}\r\n", this.WeatherType1.ToString() + "转" + this.WeatherType2.ToString());
            }
            
            sb.AppendFormat("温度:{0}\r\n", this.Temperature);
            sb.AppendFormat("最高温度:{0}\r\n", this.TemperatureMax);
            sb.AppendFormat("最低温度:{0}\r\n", this.TemperatureMin);
            sb.AppendFormat("风:{0}\r\n", this.Wind);
            sb.AppendFormat("穿衣指数:{0}\r\n", this.Costume);
            sb.AppendFormat("晨练指数:{0}\r\n", this.MorningExercise);

            sb.AppendFormat("舒适度指数:{0}\r\n", this.Comfort);
            sb.AppendFormat("感冒指数:{0}\r\n", this.Cold);
            sb.AppendFormat("洗车指数:{0}\r\n", this.CarWash);
            sb.AppendFormat("交通指数:{0}\r\n", this.Traffic);
            sb.AppendFormat("紫外线强度指数:{0}\r\n", this.Ultraviolet);
            sb.AppendFormat("空气污染扩散条件指数:{0}\r\n", this.AirPollution);

            return sb.ToString();
        }

    }
}
